package org.example.postgraduateproject.Dao.imp;

import org.example.postgraduateproject.Dao.QuestionDao;
import org.example.postgraduateproject.Utils.DbUtils;
import org.example.postgraduateproject.Utils.jdbcUtils;
import org.example.postgraduateproject.entity.Question;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wu
 * @date 2025-05-28
 */
public class QuestionDaoImp implements QuestionDao {
    @Override
    public List<Question> selectAllQuestions() {
        StringBuilder sql = new StringBuilder("SELECT question_id, user_id, title, content, created_at, updated_at, status, view_count FROM questions;");
        List<Question> questions = new ArrayList<>();
        Connection conn = DbUtils.getConnection();
        ResultSet rst = null;
        try {
            rst = jdbcUtils.executeQuery(conn, sql);
            while (rst.next()) {
                Question question = new Question();
                question.setQuestionId(rst.getInt("question_id"));
                question.setUserId(rst.getInt("user_id"));
                question.setTitle(rst.getString("title"));
                question.setContent(rst.getString("content"));
                question.setCreatedAt(rst.getDate("created_at"));
                question.setUpdatedAt(rst.getDate("updated_at"));
                question.setStatus(rst.getString("status"));
                question.setViewCount(rst.getInt("view_count"));
                questions.add(question);
            }
            return questions;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(rst, conn, null);
        }
    }

    @Override
    public Question selectQuestionById(int questionId) {
        StringBuilder sql = new StringBuilder("SELECT question_id, user_id, title, content, created_at, updated_at, status, view_count FROM questions WHERE question_id = ?;");
        Connection conn = DbUtils.getConnection();
        ResultSet rst = null;
        try {
            rst = jdbcUtils.executeQuery(conn, sql, questionId);
            if (rst.next()) {
                Question question = new Question();
                question.setQuestionId(rst.getInt("question_id"));
                question.setUserId(rst.getInt("user_id"));
                question.setTitle(rst.getString("title"));
                question.setContent(rst.getString("content"));
                question.setCreatedAt(rst.getDate("created_at"));
                question.setUpdatedAt(rst.getDate("updated_at"));
                question.setStatus(rst.getString("status"));
                question.setViewCount(rst.getInt("view_count"));
                return question;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(rst, conn, null);
        }
        return null;
    }

    @Override
    public List<Question> selectQuestionsByUserId(int userId) {
        StringBuilder sql = new StringBuilder("SELECT question_id, user_id, title, content, created_at, updated_at, status, view_count FROM questions WHERE user_id = ?;");
        List<Question> questions = new ArrayList<>();
        Connection conn = DbUtils.getConnection();
        ResultSet rst = null;
        try {
            rst = jdbcUtils.executeQuery(conn, sql, userId);
            while (rst.next()) {
                Question question = new Question();
                question.setQuestionId(rst.getInt("question_id"));
                question.setUserId(rst.getInt("user_id"));
                question.setTitle(rst.getString("title"));
                question.setContent(rst.getString("content"));
                question.setCreatedAt(rst.getDate("created_at"));
                question.setUpdatedAt(rst.getDate("updated_at"));
                question.setStatus(rst.getString("status"));
                question.setViewCount(rst.getInt("view_count"));
                questions.add(question);
            }
            return questions;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(rst, conn, null);
        }
    }

    @Override
    public int insertQuestion(Question question) {
        StringBuilder sql = new StringBuilder("INSERT INTO questions (user_id, title, content, status, view_count) VALUES (?, ?, ?, ?, ?);");
        Connection conn = DbUtils.getConnection();
        try {
            return jdbcUtils.executeUpdate(conn, sql,
                    question.getUserId(),
                    question.getTitle(),
                    question.getContent(),
                    question.getStatus(),
                    question.getViewCount()
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(null, conn, null);
        }
    }

    @Override
    public int updateQuestion(Question question) {
        StringBuilder sql = new StringBuilder("UPDATE questions SET title = ?, content = ?, status = ?, view_count = ?, updated_at = NOW() WHERE question_id = ?;");
        Connection conn = DbUtils.getConnection();
        try {
            return jdbcUtils.executeUpdate(conn, sql,
                    question.getTitle(),
                    question.getContent(),
                    question.getStatus(),
                    question.getViewCount(),
                    question.getQuestionId()
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(null, conn, null);
        }
    }

    @Override
    public int deleteQuestion(int questionId) {
        StringBuilder sql = new StringBuilder("DELETE FROM questions WHERE question_id = ?;");
        Connection conn = DbUtils.getConnection();
        try {
            return jdbcUtils.executeUpdate(conn, sql, questionId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(null, conn, null);
        }
    }
}